package com.zzty.zzlp.controller;

import com.zzty.zzlp.entity.Article;
import com.zzty.zzlp.entity.ShowItem;
import com.zzty.zzlp.entity.Twitter;
import com.zzty.zzlp.entity.User;
import com.zzty.zzlp.service.EditService;
import com.zzty.zzlp.service.TwitterService;
import com.zzty.zzlp.service.UserService;
import com.zzty.zzlp.service.User_infoService;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;


@Controller
@RequestMapping("/Twitter")
public class TwitterController {

    @Autowired
    TwitterService twitterService;
    @Autowired
    EditService editService;
    @Autowired
    UserService userService;
    @Autowired
    User_infoService userInfoService;


    @RequestMapping(value="/searchTwitter",method = RequestMethod.GET)
    @ResponseBody
    public Twitter searchAllTwitter(HttpServletRequest request, HttpServletResponse response) throws  Exception{

        ShowItem si = new ShowItem();
        User user = (User) request.getSession().getAttribute("User");
        List<Twitter> twitters = new ArrayList();
        twitters = twitterService.getImg();
        JSONArray jsonArray = new JSONArray();
        String type;
        for(Twitter attribute : twitters){
            JSONObject json = new JSONObject();
            si.setId(attribute.getId());
            String avatar = userInfoService.selectByPrimaryKey(attribute.getAuthor()).getPhoto();
            si.setAuthor(userService.selectUserById(attribute.getAuthor()).getName());
            si.setContent(attribute.getContent());
            si.setTime(attribute.getTime().substring(0,19));
            List<String> imgList = attribute.getImgs();
//            System.out.println("imgList=========="+imgList);
//            System.out.println("size==============="+imgList.size());
//            System.out.println("isEmpty========"+imgList.isEmpty());
//            System.out.println("isNull========"+imgList.get(0)!= "");
//            System.out.println("imgList[0]======"+imgList.get(0).length());
            type = "twitter";
            String bcss;
            String display;
            if ( imgList.get(0).length()>1  && "twitter".equals(type) ) {
                display = "true";
                bcss = "BriefWithImg";
            } else {
                display = "false";
                bcss = "Layer22";
            }
            if(imgList.size() == 1 &&imgList.get(0).length() <= 1){
                json.put("imgs",null);
                json.put("dimgs",null);
            }else{
                json.put("imgs",imgList);
                json.put("dimgs",imgList);
            }

            json.put("name",si.getAuthor());
            json.put("avatar",avatar);
            json.put("content",si.getContent());
            json.put("title",si.getContent());
            json.put("time",si.getTime());
            json.put("bcss",bcss);
            json.put("display",display);
            json.put("type",type);

            jsonArray.add(json);

        }
        List<Article> articles = new ArrayList();
        articles = editService.getImg();
        for(Article attribute : articles){
            JSONObject json = new JSONObject();
            si.setAuthor(userService.selectUserById(attribute.getUserId()).getName());
            si.setTitle(attribute.getTitle());
            si.setImgs(attribute.getImgs());
            si.setTime(attribute.getTime().substring(0,19));
            si.setContent(attribute.getContent());
            si.setComment(attribute.getComment());
            String avatar = userInfoService.selectByPrimaryKey(attribute.getUserId()).getPhoto();
            type = "article";
            String bcss;
            String display;
            String urlDisplay;
            System.out.println("ArticleUrl========="+si.getComment());
            System.out.println("ArticleUrlSize=========="+si.getComment().length());
            if (si.getImgs() != null && "article".equals(type) && !si.getImgs().isEmpty() && si.getImgs().size() >= 1) {
                display = "true";
                bcss = "BriefWithImg";
            } else {
                display = "false";
                bcss = "Layer22";
            }
            if(si.getComment() != null && si.getComment().length() >0){
                urlDisplay = "true";
            }else {
                urlDisplay = "false";
            }

            json.put("name",si.getAuthor());
            json.put("avatar",avatar);
            json.put("title",si.getTitle());
            json.put("content",si.getContent());
            json.put("imgs",si.getImgs());
            json.put("time",si.getTime());
            json.put("bcss",bcss);
            json.put("display",display);
            json.put("type",type);
            json.put("articleUrl",si.getComment());
            json.put("urlDisplay",urlDisplay);

            jsonArray.add(json);
        }
        System.out.println("123============"+jsonArray.toString());
        response.setCharacterEncoding("UTF-8");
        response.flushBuffer();
        response.getWriter().write(jsonArray.toString());
        response.getWriter().flush();
        response.getWriter().close();
        return null;
    }

    @RequestMapping(value="/searchSelf",method = RequestMethod.GET)
    @ResponseBody
    public Twitter searchSelf(HttpServletRequest request, HttpServletResponse response) throws  Exception{



        ShowItem si2 = new ShowItem();
        User user = (User) request.getSession().getAttribute("User");
        String selfName = user.getName();
        System.out.println("selfName========="+selfName);
        String selfAvatar = userInfoService.selectByPrimaryKey(user.getId()).getPhoto();
        List<Twitter> twitters = new ArrayList();
        twitters = twitterService.getSelfImg(user.getId());
        JSONArray jsonArrays = new JSONArray();
        String type;
        for(Twitter attribute : twitters){
            JSONObject json = new JSONObject();
            si2.setId(attribute.getId());
            String avatar = userInfoService.selectByPrimaryKey(attribute.getAuthor()).getPhoto();
            si2.setAuthor(userService.selectUserById(attribute.getAuthor()).getName());
            si2.setContent(attribute.getContent());
            si2.setTime(attribute.getTime().substring(0,19));
            List<String> imgList = attribute.getImgs();
            type = "twitter";
            String bcss;
            String display;
            if ( imgList.get(0).length()>1  && "twitter".equals(type) ) {
                display = "true";
                bcss = "BriefWithImg";
            } else {
                display = "false";
                bcss = "Layer22";
            }
            if(imgList.size() == 1 &&imgList.get(0).length() <= 1){
                json.put("imgs",null);
                json.put("dimgs",null);
            }else{
                json.put("imgs",imgList);
                json.put("dimgs",imgList);
            }

            json.put("selfName",selfName);
            json.put("selfAvatar",selfAvatar);
            json.put("name",si2.getAuthor());
            json.put("avatar",avatar);
            json.put("content",si2.getContent());
            json.put("title",si2.getContent());
            json.put("time",si2.getTime());
            json.put("bcss",bcss);
            json.put("display",display);
            json.put("type",type);

            jsonArrays.add(json);

        }
        List<Article> articles = new ArrayList();
        articles = editService.getSelfImg(user.getId());
        for(Article attribute : articles){
            JSONObject json = new JSONObject();
            si2.setAuthor(userService.selectUserById(attribute.getUserId()).getName());
            si2.setTitle(attribute.getTitle());
            si2.setImgs(attribute.getImgs());
            si2.setTime(attribute.getTime().substring(0,19));
            si2.setContent(attribute.getContent());
            si2.setComment(attribute.getComment());
            String avatar = userInfoService.selectByPrimaryKey(attribute.getUserId()).getPhoto();
            type = "article";
            String bcss;
            String display;
            String urlDisplay;
            if (si2.getImgs() != null && "article".equals(type) && !si2.getImgs().isEmpty() && si2.getImgs().size()>= 1) {
                display = "true";
                bcss = "BriefWithImg";
            } else {
                display = "false";
                bcss = "Layer22";
            }

          if(si2.getComment() != null && si2.getComment().length() >0){
            urlDisplay = "true";
        }else {
            urlDisplay = "false";
        };
            json.put("name",si2.getAuthor());
            json.put("avatar",avatar);
            json.put("title",si2.getTitle());
            json.put("content",si2.getContent());
            json.put("imgs",si2.getImgs());
            json.put("time",si2.getTime());
            json.put("articleUrl",si2.getComment());
            json.put("bcss",bcss);
            json.put("display",display);
            json.put("type",type);
            json.put("urlDisplay",urlDisplay);

            jsonArrays.add(json);
        }
        System.out.println("456==========="+jsonArrays.toString());
        System.out.println(jsonArrays.isEmpty());
        if(jsonArrays.isEmpty()){
            JSONObject json = new JSONObject();
            json.put("selfName",selfName);
            json.put("selfAvatar",selfAvatar);
            jsonArrays.add(json);
        }
        System.out.println("789============"+jsonArrays.toString());
        response.setCharacterEncoding("UTF-8");
        response.flushBuffer();
        response.getWriter().write(jsonArrays.toString());
        response.getWriter().flush();
        response.getWriter().close();
        return null;
    }

    @RequestMapping(value="/mainHtmlAva",method = RequestMethod.GET)
    @ResponseBody
    public String getMainAva(HttpServletRequest request, HttpServletResponse response) throws  Exception{
        User user =(User) request.getSession().getAttribute("User");
        String mainAva = userInfoService.selectByPrimaryKey(user.getId()).getPhoto();
        JSONArray jsonArray = new JSONArray();
        JSONObject json = new JSONObject();
        json.put("mainAva",mainAva);
        jsonArray.add(json);
        System.out.println("mainAva============"+jsonArray.toString());
        response.setCharacterEncoding("UTF-8");
        response.flushBuffer();
        response.getWriter().write(jsonArray.toString());
        response.getWriter().flush();
        response.getWriter().close();
        return null;
    }
}
